<?php

namespace api\modules\v1\controllers\statistics;

use Yii;
use common\helpers\DateHelper;
use common\models\base\Profession;
use common\models\clinic\Clinic;
use common\models\member\Attr;
use common\models\member\Member;
use common\models\clinic\UserTreatment;
use api\controllers\OnAuthController;
use yii\web\HttpException;

class FollowController extends OnAuthController
{

    public $modelClass = UserTreatment::class;


    /**
     * 获取下拉信息
     *
     * @author wangxiang
     * @return array
     */
    public function actionSelectInfo()
    {
        $clinic = Yii::$app->services->clinic->findList([
            'select' => ['id', 'name'],
            'project_id' => $this->projectId,
            'status' => Clinic::STATUS_ENABLE,
        ]);

        if (Member::IS_SUPER_NO == Yii::$app->user->identity->member->is_super) {
            $attr = Yii::$app->services->attr->findInfo([
                'member_id' => Yii::$app->user->identity->member_id,
                'project_id' => $this->projectId,
            ]);
            if ($attr->is_department_leader == Attr::IS_DEPARTMENT_LEADER_NO) {// 普通员工【自己】
                $memberList = [Yii::$app->user->identity->member_id];
                $clinic = Yii::$app->services->clinic->findList([
                    'select' => ['id', 'name'],
                    'project_id' => $this->projectId,
                    'status' => Clinic::STATUS_ENABLE,
                    'id' => $attr->clinic_id,
                ]);
            } elseif ($attr->is_department_leader == Attr::IS_DEPARTMENT_LEADER_YES) {// 部门负责人【部门下所有员工】
                $attrList = Yii::$app->services->attr->findList([
                    'project_id' => $this->projectId,
                    'department_id' => $attr->department_id,
                    'profession_id' => Profession::ID_MEDICAL,
                ]);
                $memberList = array_column($attrList, 'member_id');
                $clinic = Yii::$app->services->clinic->findList([
                    'select' => ['id', 'name'],
                    'project_id' => $this->projectId,
                    'status' => Clinic::STATUS_ENABLE,
                    'id' => $attr->clinic_id,
                ]);
            } else {// 项目负责人【项目下员工】
                $attrList = Yii::$app->services->attr->findList([
                    'project_id' => $this->projectId,
                    'profession_id' => Profession::ID_MEDICAL,
                ]);
                $memberList = array_column($attrList, 'member_id');
            }
        } else {
            $attrList = Yii::$app->services->attr->findList([
                'project_id' => $this->projectId,
                'profession_id' => Profession::ID_MEDICAL,
            ]);
            $memberList = array_column($attrList, 'member_id');
        }


        return [
            'member_medical_list' => Yii::$app->services->member->findList([
                'select' => ['id', 'realname AS name'],
                'id' => $memberList,
                'status' => Member::STATUS_ENABLE
            ]),
            'clinic' => $clinic
        ];
    }


    /**
     * 获取跟进列表数据
     *
     * @author wangxiang
     * @return array
     * @throws HttpException
     */
    public function actionList()
    {
        $year = Yii::$app->request->get('year', date('Y'));
        if (!$year) {
            throw new HttpException(422, '年份不能为空！');
        }
        $yearTime = DateHelper::aYear($year);
        $page = (int)Yii::$app->request->get('page', 1);
        $pageSize = (int)Yii::$app->request->get('pageSize', 20);
        $params = [
            'project_id' => $this->projectId,
            'start_time' => $yearTime['start'],
            'end_time' => $yearTime['end'],
            'keyword' => trim((string)Yii::$app->request->get('keyword', '')),
            'clinic_id' => (int)Yii::$app->request->get('clinic_id', 0),
            'belong_assistant_id' => (int)Yii::$app->request->get('belong_assistant_id', 0),

        ];

        return Yii::$app->services->followReport->getFollowList($params, $page, $pageSize);
    }


    /**
     * 获取报表数据（非实时）
     *
     * @author wangxiang
     * @return array
     * @throws HttpException
     */
    public function actionReport()
    {
        $year = Yii::$app->request->get('year', date('Y'));
        if (!$year) {
            throw new HttpException(422, '年份不能为空！');
        }
        $page = (int)Yii::$app->request->get('page', 1);
        $pageSize = (int)Yii::$app->request->get('pageSize', 20);
        $params = [
            'project_id' => $this->projectId,
            'year' => $year,
            'keyword' => trim((string)Yii::$app->request->get('keyword', '')),
            'clinic_id' => (int)Yii::$app->request->get('clinic_id', 0),
            'belong_assistant_id' => (int)Yii::$app->request->get('belong_assistant_id', 0),
            'sort_field' => (string)Yii::$app->request->get('sort_field', ''),
            'sort' => (int)Yii::$app->request->get('sort', 0),
        ];

        // 获取报表数据
        $data = Yii::$app->services->followReport->getFollowReport($params, $page, $pageSize);

        // 格式化列表数据
        $data['list'] = Yii::$app->services->followReport->formatReport($data['list']);

        return $data;
    }

}
